package com.example.blog.web.adminController;

import com.example.blog.pojo.Blog;
import com.example.blog.pojo.BlogQuery;
import com.example.blog.pojo.User;
import com.example.blog.service.BlogService;
import com.example.blog.service.TagService;
import com.example.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    TypeService typeService;
    @Autowired
    private BlogService blogService;
    @Autowired
    TagService tagService;
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 6,sort = {"updateTime"},direction = Sort.Direction.ASC)Pageable pageable,
                        BlogQuery blog, Model model){
        model.addAttribute("types",typeService.list());

        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs";
    }
    @PostMapping("/blogs/search")
    public String blogsSearch(@PageableDefault(size = 6,sort = {"updateTime"},direction = Sort.Direction.ASC)Pageable pageable,
                        BlogQuery blog, Model model){
        System.out.println(pageable.getPageNumber());
            model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs :: blogList";
    }
    @GetMapping("blogs/input")
    public String input(Model model){
        model.addAttribute("types",typeService.list());
        model.addAttribute("tags",tagService.listTag());

        return "admin/blog-input";
    }
    @PostMapping("/blogs/save")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTag(tagService.List(blog.getTagIds()));
        Blog b = blogService.saveBlog(blog);
        if(b == null){
            attributes.addFlashAttribute("message","操作失败");
        }
        else{
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id , Model model){
        model.addAttribute("types",typeService.list());
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("blog",blogService.getBLog(id));
        return "admin/blog-Edit-input";
    }
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id){
       blogService.deleteBlog(id);
        return "redirect:/admin/blogs";
    }
    @PostMapping("/blogs/fileupload")
    public String fileupload(HttpServletRequest request, MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();  // 文件名
        String osName = System.getProperty("os.name");
        String realPath;
        System.out.println(osName.toLowerCase());
        if (osName.toLowerCase().startsWith("win")) {
            realPath = "/static/img/";
        } else {
            realPath = "/resources/community/upload/";
        }

        File f = new File(osName);
        if(!f.isDirectory()) f.mkdir();
        File dest = new File(realPath,fileName);

        file.transferTo(dest);
        return "admin/imgUpload";
    }
    @GetMapping("blogs/file")
    public String file(){
        return "admin/imgUpload";
    }
}

